package q622_MyCircularQueue;

import CommonClass.Common.ListNode;

/*
基于链表实现队列
用一个size来记录当前队列长度
第一个元素插入时 将head和tail都指向它
然后随着元素插入 将tail后移
每次推出时 将head后移即可
 */
public class MyCircularQueue {
    private ListNode head;
    private ListNode tail;
    private int capacity;
    private int size;

    public MyCircularQueue(int k) {
        size = 0;
        capacity = k;
    }

    public boolean enQueue(int value) {
        if (isFull()) return false;
        ListNode node = new ListNode(value);
        if (isEmpty()) {
            head = tail = node;
        } else {
            tail.next = node;
            tail = tail.next;
        }
        ++size;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) return false;
        head = head.next;
        --size;
        return true;
    }

    public int Front() {
        if (isEmpty()) return -1;
        return head.val;
    }

    public int Rear() {
        if (isEmpty()) return -1;
        return tail.val;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == capacity;
    }
}
